﻿using Common;
using log4net;
using Microsoft.Extensions.DependencyInjection;
using ModelDto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Common.Achieve
{
    public class log4netHelper : Ilog4netHelper
    {
        //private static log4net.ILog LogInfo = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        private ILog LogInfo { get; set; }

        public log4netHelper()
        {

        }
        public void SetLoggerName(LogNameEnum logNameEnum)
        {
            switch (logNameEnum)
            {
                case LogNameEnum.ErrorLog:
                    LogInfo = LogManager.GetLogger("ErrorLog");
                    break;
                case LogNameEnum.InfoLog:
                    LogInfo = LogManager.GetLogger("InfoLog");
                    break;
            }
        }

        public void SetConfig(FileInfo configFile)
        {
            log4net.Config.XmlConfigurator.Configure(configFile);
        }

        public void Error(string msg = "出现异常", Exception ex = null)
        {
            LogInfo = LogManager.GetLogger("ErrorLog");
            try
            {
                if (LogInfo.IsErrorEnabled)
                {
                    LogInfo.Error(msg, ex);
                }
            }
            catch { }
        }

        /// <summary>
        /// Log4日志Warn
        /// </summary>
        /// <param name="msg"></param>
        public void Warn(string msg)
        {
            //Console.WriteLine(msg);
            LogInfo.Warn(msg);
        }

        /// <summary>
        /// Log4日志Info
        /// </summary>
        /// <param name="msg"></param>
        public void Info(string msg)
        {
            LogInfo = LogManager.GetLogger("InfoLog");
            //Console.WriteLine(msg);
            LogInfo.Info(msg);
        }

        /// <summary>
        /// Log4日志Debug
        /// </summary>
        /// <param name="msg"></param>
        public void Debug(string msg)
        {
            //Console.WriteLine(msg);
            LogInfo.Debug(msg);
        }


    }
}
